//
//  MessageViewController.swift
//  甜言蜜语
//
//  Created by 王剑伟 on 16/6/17.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit

class MessageViewController: UIViewController {

    @IBOutlet weak var pageContainerView: UIScrollView!
    @IBOutlet weak var pageSelect: UISegmentedControl!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.pageContainerView.sizeToFit()
        self.tabBarController?.tabBar.tintColor = NAVIGATIONBAR_RED_COLOR
        self.title = "消息"
        // Do any additional setup after loading the view.
        var frame: CGRect = CGRectMake(0, 0, 0, 0)
        
        self.pageContainerView.pagingEnabled = true
        self.pageContainerView.contentSize = CGSizeMake(UIScreen.mainScreen().bounds.width * 2, self.pageContainerView.frame.size.height)
        
        let answerVC = self.storyboard?.instantiateViewControllerWithIdentifier("AnswerTableViewController") as! AnswerTableViewController
        
        frame.origin.x = 0
        frame.size = CGSizeMake(self.pageContainerView.frame.size.width, self.pageContainerView.frame.size.height)
        
        answerVC.tableView.frame = frame
        
        self.addChildViewController(answerVC)
        self.pageContainerView.addSubview(answerVC.tableView)
        
        let systemVC = self.storyboard?.instantiateViewControllerWithIdentifier("SystemViewController") as! SystemViewController
        
        frame.origin.x = UIScreen.mainScreen().bounds.width
        frame.size = CGSizeMake(self.pageContainerView.frame.size.width, self.pageContainerView.frame.size.height)
        
        systemVC.view.frame = frame
        
        self.addChildViewController(systemVC)
        self.pageContainerView.addSubview(systemVC.view)
        
        self.pageContainerView.delegate = self
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        if ApplicationDelegate.isLogin(){
            let phone = publicMethod.userName()
            let pass = publicMethod.passWord()
            let token = publicMethod.token()
            DataTool.login(phone, pass: pass, token: token, completionHandler: { (newModel) in
                guard let newDataes = newModel else {
                    return
                }
                print(newDataes)
                
            })
            
        }
        
        self.tabBarController?.tabBar.hidden = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func changePage(sender: UISegmentedControl) {
        var offset = CGPointMake(0, 0)
        
        if sender.selectedSegmentIndex != 0 {
            offset.x = UIScreen.mainScreen().bounds.width
        }
        
        self.pageContainerView.setContentOffset(offset, animated: true)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension MessageViewController: UIScrollViewDelegate {
    /**
     这个是在newsContainerView减速停止的时候开始执行，
     用来切换需要显示的新闻列表和让频道标签处于合适的位置
     */
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        if scrollView.contentOffset.x > 0 {
            self.pageSelect.selectedSegmentIndex = 1
        } else {
            self.pageSelect.selectedSegmentIndex = 0
        }
    }
}
